package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityMessageDetail;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanArticle;
import nero.yncc.com.createart.bean.BeanMessage;
import nero.yncc.com.createart.fragment.FragmentFound;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.TimeUtils;

/**
 * Created by nero on 17-6-21.
 */

public class AdapterFoundArticle extends BaseAdapter {

    public static int viewHeight = 0;

    Context context;
    ArrayList<BeanMessage> arrayList;
    public AdapterFoundArticle(Context context,ArrayList<BeanMessage> arrayList) {
        this.context = context;
        this.arrayList = arrayList;

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            view = LinearLayout.inflate(context, R.layout.adapter_found_article, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        if (viewHeight == 0) {
            view.measure(0, 0);
            viewHeight = view.getMeasuredHeight();

        }

        final BeanMessage bean = arrayList.get(i);
        holder.tv_title.setText(bean.getTitle());
        holder.tv_content.setText( TimeUtils.getDate(bean.getPublish_time()));
        ImageUtils.BindImage(context, holder.iv_image, Constants.IMAGE_HOST+ bean.getThumb(),4);
//        if(i==arrayList.size()-1 && fragmentFound!=null){
//            fragmentFound.showMore();
//        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ActivityMessageDetail.class);
                intent.putExtra("message",bean);
                context.startActivity(intent);
            }
        });
        return view;
    }

    class Holder {
        public Holder(View itemView) {
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
        }

        final ImageView iv_image;
        final TextView tv_title, tv_content;
    }
}
